# Send an Ajax request
#
# @param action action name
# @param data object data
# @param func callback function
# @param secure security mode
# @example send('getwidget', {key1:'value1', key2:'value2'}, function(result) { your code }, false);
# @default secure = true
send = (action, data, func, secure) ->
    secure = secure or false
    syn = app.root + "/syn/"
    if secure
        data.uid = app.uid
        data.token = app.token
    url = syn + action.toLowerCase()
    $.ajax(
        url: url
        type: "POST"
        async: false
        data: data
    ).success (result) ->
        func result
        return
    return

# Render HTML template
#
# @param template id of element
# @param data object
# @example <div class="template" id="abc"><div class="{key1}">{key2}</div></div>
# @example render('abc', {key1:'name', key2:'Hello My name is Loi'});
render = (template, data) ->
    html = $("#" + template).html()
    pattern = /\{[\sa-z0-9_]+\}/img
    result = ""
    var_ = ''
    index = 0

    while match = pattern.exec(html)
        var_ = match[0].substring(1, match[0].length - 1).trim()
        result += html.substring(index, match.index)
        eval_ "result+=data." + var_
        index = pattern.lastIndex
    result += html.substring(index, html.length)
    result
